package com.xsomnus.open.im.authv2.codec;

import com.xsomnus.open.im.protocol.codec.PacketCodec;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;

/**
 * @author @xsomnus666_xiawenye★
 * @since 2019/4/25 0025 16:04
 * 当你发现自己的才华撑不起野心时，就请安静下来学习吧
 */
public class Spliter extends LengthFieldBasedFrameDecoder {

    private static final int LENGTH_FIELD_OFFSET = 7;
    private static final int LENGTH_FIELD_LENGTH = 4;


    public Spliter() {
        super(Integer.MAX_VALUE, LENGTH_FIELD_OFFSET, LENGTH_FIELD_LENGTH);
    }

    @Override
    protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {

        if (in.getInt(in.readerIndex()) != PacketCodec.MAGIC_NUMBER) {
            System.out.println("协议非法~");
            ctx.channel().close();
            return null;
        }

        return super.decode(ctx, in);
    }
}
